<template>
<div class="login_container">
    <div class="login_box">
<!--      头像-->
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="">
      </div>

      <el-form :model="loginFrom" :rules="loginFromRules" ref="LoginFromRef"  label-width="0px" class="login_from">
        <el-form-item label="" prop="username">
          <el-input v-model="loginFrom.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>

        <el-form-item label="" prop="password">
          <el-input type="password" v-model="loginFrom.password"  prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>

        <el-form-item label="" class="buts">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="LoginReset">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
</div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 表单数据绑定
      loginFrom: {
        username: 'admin',
        password: '123456',
      },
      // 表达验证规则
      loginFromRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods:{
    // 重置输入
    LoginReset(){
      this.$refs.LoginFromRef.resetFields();
    },
    // 处理登陆
    login(){
      this.$refs.LoginFromRef.validate( async valid=>{  // 验证表达数据是否通过
        if (!valid){
          return ;
        }
        const {data : res} = await this.$http.post('login',this.loginFrom) // 发起请求
        if (res.meta.status !== 200){
          return this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        // 保存token信息
        window.sessionStorage.setItem('token',res.data.token)
        await this.$router.push("/home") // 跳转
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login_container{
    background-color: #2b4b6b;
    height: 100%;
  }
  .login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);

    .avatar-box{
      height: 130px;
      width: 130px;
      border: 1px solid #eeeeee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      background-color: #FFFFFF;
      img{
        height: 130px;
        width: 130px;
        border-radius: 50%;
        background-color: #eeeeee;
      }
    }
  }
  .buts{
    display: flex;
    justify-content: flex-end;
  }
  .login_from{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
  }
</style>
